/*
Description:    FIUMAPS.Identify allows users to click on the map to get information.
Author:         Carlos Krefft (krefftc@gmail.com)

*/

FIUMAPS.Identify = {Active:false,
    IdentifyPoint: function (overlay, latLng) {     
        this.Active = true;
     
        if(overlay){return;}
        
        if (FIUMAPS.Overlays.Data){
            if(FIUMAPS.Overlays.Data.length == 1){
                FIUMAPS.Identify.clearResults();
            }
        } 
        // set the identify parameters
        var identifyParameters = new esri.arcgis.gmaps.IdentifyParameters();
        identifyParameters.geometry = latLng; // location where the user clicked on the map
        identifyParameters.tolerance = 3; //The distance in screen pixels from the specified geometry within which the identify is performed.
        identifyParameters.layerIds = [ 1 ]; // only identify the buildings.
        identifyParameters.layerOption = "all";
        identifyParameters.bounds = gmap.getBounds();
        var mapSize = gmap.getSize();
        identifyParameters.width = mapSize.width;
        identifyParameters.height = mapSize.height;

        // execute the identify operation
        identifyTask.execute(identifyParameters, function(response, error) { // function to be called when the result is available
            // display error message (if any) and return
            if (FIUMAPS.Identify.hasErrorOccurred(error)) {return;}
            
            // note that the location where the user clicked on the map (latLng) is visible in this function through closure
            FIUMAPS.Identify.addResultToMap(response, latLng);
        });
    },  
    hasErrorOccurred: function (error) {
        if (error) {
            alert("Error " + error.code + ": " + (error.message || (error.details && error.details.join(" ")) || "Unknown error" ));
            return true;
        }
        return false;
    },     
    addResultToMap: function (response, point) {

        var idResults = response.identifyResults;

        //pull building. index = 0 because buildings cannot be overlapping so you should never have more than 1.
        var results = idResults[0];
        //Check wether building was identified:
        if(results){     
            FIUMAPS.Identify.clearResults();    
            FIUMAPS.Search.ResetResults();
            FIUMAPS.LocationsByName.Reset();
            accordionResize(current_tab);
            //if(!mymapLocations.isActive) mymapLocations.hideGeometries();
             
            var attributes = results.feature.attributes;
            var geometry = results.feature.geometry;
            
            FIUMAPS.HTMLPopUp.Center = geometry[0].getBounds().getCenter();
            FIUMAPS.HTMLPopUp.openPolygonPopup(attributes,true);
                                   
            addPolygonListeners(geometry,attributes,null);             
            
        }        
        
    },  
    clearResults: function () {
        //gmap.closeInfoWindow();
        ClearOverlays();
    }
  
};